//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.Expressions
{
    sealed class AnchoredRowReference: RowReference
    {
        public AnchoredRowReference(Row row, bool isLocked)
        {
            _anchor = row;
            _islocked = isLocked;
        }
        internal override string GetText(TextContext context)
        {
            return Label;
        }

        internal override RowReference Projected(Worksheet targetWorksheet)
        {
            return new AnchoredRowReference(targetWorksheet.Rows[Anchor.Index], IsLocked);
        }

        internal override DimensionReference<Column, Row> SwapDimension()
        {
            return new AnchoredColumnReference(Worksheet.Columns[this.Anchor.Index], IsLocked);
        }

        internal override RowReference Offset(CellRelativeAddress address)
        {
            if (_islocked || address.Row == 0)
                return this;
            return new AnchoredRowReference(Worksheet.Rows[Anchor.Index + address.Row], IsLocked);
        }
        private readonly Row _anchor;

        public override Row Anchor
        {
            get { return _anchor; }
        }
        private readonly bool _islocked;

        internal override bool IsLocked
        {
            get { return _islocked; }
        }


        internal override Row GetRow(EvaluationContext context)
        {
            if (IsLocked || context.Offset.Row == 0)
                return Anchor;
            return Worksheet.Rows[Anchor.Index + context.Offset.Row];
        }

        public override string Label
        {
            get { if (IsLocked)
                return "$" + Anchor.Label;
            return Anchor.Label;
        }
        }

        public override Worksheet Worksheet
        {
            get { return Anchor.Worksheet; }
        }
    }
}
